package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivityReceiveValueBinding
import com.play.ballen.me.adapter.MoneyLogAdapter
import com.play.ballen.me.dialog.OperateSuccessDialogUtils
import com.play.ballen.me.vm.ReceiveValueVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class ReceiveValueActivity : BaseStdTitleActivity<ReceiveValueVM, ActivityReceiveValueBinding>() {

    var type = ""

    private val adapter: MoneyLogAdapter by lazy {
        MoneyLogAdapter(arrayListOf())
    }

    companion object {
        const val TYPE_MAIN_VALUE = "0"
        const val TYPE_SPORT_VALUE = "1"

        @JvmStatic
        fun start(context: Context, type: String) {
            val starter = Intent(context, ReceiveValueActivity::class.java)
            starter.putExtra("type", type)
            context.startActivity(starter)
        }
    }

    private val operateSuccessDialogUtils by lazy { OperateSuccessDialogUtils(this) }

    override fun viewModelClass(): Class<ReceiveValueVM> {
        return ReceiveValueVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_receive_value
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.extraInfo.observe(this) {
            it.run {
                dataBinding.tvCount.text =
                    ResourceUtil.getString(R.string.price_unit_bmt, money)
                dataBinding.tvServiceFee.text =
                    ResourceUtil.getString(R.string.price_unit_bmt, fee)
                dataBinding.tvActually.text =
                    ResourceUtil.getString(R.string.price_unit_bmt, receipt)
            }
        }

        viewModel.actionCommitSuccess.observe(this) {
            operateSuccessDialogUtils.show(ResourceUtil.getString(R.string.receive_success))
            //刷新页面
            viewModel.extraInfo(type)
            dataBinding.rv.scrollToPosition(0)
            dataBinding.refreshLayout.autoRefresh()
        }

        viewModel.actionGetMoneyLogFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (viewModel.page == 1) {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
            } else {
                ToastUtils.show(ResourceUtil.getString(R.string.load_fail))
            }
        }

        viewModel.listMoneyLog.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()

            if (!it.isNullOrEmpty()) {
                if (viewModel.page == 1) {
                    adapter.setList(it)

                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                } else {
                    adapter.addData(it)
                }
                viewModel.page++
            } else {
                //no data
                if (viewModel.page == 1) {
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                } else {
                    ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                }
            }
        }
    }

    override fun initView() {
        super.initView()
        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            loadMoneyLogList()
        }

        dataBinding.btnReceive.setOnSingleClickListener tag@{
            if (viewModel.extraInfo.value == null) {
                return@tag
            }
            viewModel.extraConfirm(type)
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                loadMoneyLogList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                loadMoneyLogList()
            }
        })

        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter

        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
        viewModel.page = 1

        viewModel.extraInfo(type)
        loadMoneyLogList()
    }

    private fun loadMoneyLogList() {
        viewModel.moneyLog(
            "",
            if (type == TYPE_MAIN_VALUE) {
                KVConst.MONEY_LOG_MONEY_TYPE_MAIN_VALUE
            } else {
                KVConst.MONEY_LOG_MONEY_TYPE_SPORT_VALUE
            }
        )
    }

    override fun initIntent(intent: Intent?) {
        intent?.let {
            type = it.getStringExtra("type") ?: ""
        }
    }

    override fun getToolBarTitle(): String =
        if (type == TYPE_MAIN_VALUE) {
            ResourceUtil.getString(R.string.receive_title_main)
        } else {
            ResourceUtil.getString(R.string.receive_title_sport)
        }
}